package cn.edu.scau.cmi.ema.controller;

import java.util.Set;
import java.util.List;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;
import cn.edu.scau.cmi.ema.service.pagination.*;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;

@Controller("CrawlerfilestandardetailController")
public class CrawlerfilestandardetailController extends CmiEmaController {

	@RequestMapping("/listCrawlerfilestandardetails")
	public ModelAndView listCrawlerfilestandardetails(HttpServletRequest request,
			@ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerfilestandardetailPaginationService.updatePagination(request, pagination);
		Set<Crawlerfilestandardetail> crawlerfilestandardetails = crawlerfilestandardetailPaginationService
				.getPaginationCrawlerfilestandardetails(pagination);

		mav.addObject("crawlerfilestandardetails", crawlerfilestandardetails);
		mav.addObject("entityName", "crawlerfilestandardetail");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerfilestandardetail/listCrawlerfilestandardetails.jsp");
		return mav;
	}

	public String listCrawlerfilestandardetails() {
		return "redirect:/listCrawlerfilestandardetails";
	}

	@RequestMapping("/viewCrawlerfilestandardetail")
	public ModelAndView viewCrawlerfilestandardetail(@RequestParam Integer crawlerfilestandardetailId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerfilestandardetail",
				crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId));
		mav.setViewName("crawlerfilestandardetail/viewCrawlerfilestandardetail.jsp");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerfilestandardetails")
	public ModelAndView confirmDeleteCrawlerfilestandardetails(
			@RequestParam(value = "deleteCrawlerfilestandardetailsIds", required = true) Set<Integer> deleteCrawlerfilestandardetailsIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerfilestandardetail> deleteCrawlerfilestandardetails = crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailsByIds(deleteCrawlerfilestandardetailsIds);
		Set<Crawlerfilestandardetail> referencedCrawlerfilestandardetails = crawlerfilestandardetailService
				.loadReferenceCrawlerfilestandardetails(deleteCrawlerfilestandardetails);
		boolean hasReferenced;
		if (referencedCrawlerfilestandardetails.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerfilestandardetails);
			hasReferenced = false;
		} else {
			deleteCrawlerfilestandardetails.removeAll(referencedCrawlerfilestandardetails);
			mav.addObject("referencedEntitys", referencedCrawlerfilestandardetails);
			mav.addObject("deleteEntitys", deleteCrawlerfilestandardetails);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerfilestandardetail");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerfilestandardetails");
		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestandardetail")
	public String deleteCrawlerfilestandardetail(@RequestParam Integer crawlerfilestandardetailId) {
		Crawlerfilestandardetail crawlerfilestandardetail = crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId);
		crawlerfilestandardetailService.deleteCrawlerfilestandardetail(crawlerfilestandardetail);
		return "forward:/listCrawlerfilestandardetails";
	}

	@RequestMapping("/confirmDeleteCrawlerfilestandardetail")
	public ModelAndView confirmDeleteCrawlerfilestandardetail(@RequestParam Integer crawlerfilestandardetailId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerfilestandardetail",
				crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId));

		mav.setViewName("crawlerfilestandardetail/deleteCrawlerfilestandardetail.jsp");

		return mav;
	}

	@RequestMapping("/deleteCrawlerfilestandardetails")
	public String deleteCrawlerfilestandardetails(
			@RequestParam(value = "deleteCrawlerfilestandardetailsId", required = true) Set<Integer> deleteCrawlerfilestandardetailsId) {
		crawlerfilestandardetailService.deleteCrawlerfilestandardetails(deleteCrawlerfilestandardetailsId);
		return "forward:/listCrawlerfilestandardetails";
	}

	@RequestMapping("/editCrawlerfilestandardetail")
	public ModelAndView editCrawlerfilestandardetail(@RequestParam Integer crawlerfilestandardetailId) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestandardetail crawlerfilestandardetail = crawlerfilestandardetailDAO
				.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetailId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerfilestandardetail", crawlerfilestandardetail);

		// 把所有的引用crawlerfilestandardetail对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestandardetailService
				.loadCrawlerfilestandardetailObjectReferencedSet(crawlerfilestandardetail));

		// 将编辑页面crawlerfilestandardetail添加到mav中
		mav.setViewName("crawlerfilestandardetail/editCrawlerfilestandardetail.jsp");
		return mav;
	}

	@RequestMapping("/newCrawlerfilestandardetail")
	public ModelAndView newCrawlerfilestandardetail() {
		ModelAndView mav = new ModelAndView();
		Crawlerfilestandardetail crawlerfilestandardetail = new Crawlerfilestandardetail();

		mav.addObject("crawlerfilestandardetail", crawlerfilestandardetail);

		// 把所有的引用crawlerfilestandardetail对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerfilestandardetailService
				.loadCrawlerfilestandardetailObjectReferencedSet(crawlerfilestandardetail));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerfilestandardetail/editCrawlerfilestandardetail.jsp");

		return mav;
	}

	@RequestMapping("/saveCrawlerfilestandardetail")
	public ModelAndView saveCrawlerfilestandardetail(
			@ModelAttribute Crawlerfilestandardetail crawlerfilestandardetail) {

		ModelAndView mav = new ModelAndView();

		Crawlerfilestandard crawlerfilestandardTemp = crawlerfilestandardetail.getCrawlerfilestandard();
		Crawlerfilestandard crawlerfilestandard = crawlerfilestandardTemp == null ? null
				: CmiHibernateProxyUtil
						.deproxy(crawlerfilestandardDAO.findCrawlerfilestandardById(crawlerfilestandardTemp.getId()));
		Crawlerfilestandardcolumn crawlerfilestandardcolumnTemp = crawlerfilestandardetail
				.getCrawlerfilestandardcolumn();
		Crawlerfilestandardcolumn crawlerfilestandardcolumn = crawlerfilestandardcolumnTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(crawlerfilestandardcolumnDAO
						.findCrawlerfilestandardcolumnById(crawlerfilestandardcolumnTemp.getId()));

		Crawlerfilestandardetail existingCrawlerfilestandardetail = CmiHibernateProxyUtil
				.deproxy(crawlerfilestandardetailDAO.findCrawlerfilestandardetailByPhysicalPrimaryKey(
						crawlerfilestandard.getId(), crawlerfilestandardcolumn.getId(),
						crawlerfilestandardetail.getNumber()));

		crawlerfilestandardetail.setCrawlerfilestandard(crawlerfilestandard);
		crawlerfilestandardetail.setCrawlerfilestandardcolumn(crawlerfilestandardcolumn);
		// 新建记录
		if (crawlerfilestandardetail.getId() == null) {
			if (crawlerfilestandardetail.equals(existingCrawlerfilestandardetail)) {

				mav.addAllObjects(crawlerfilestandardetailService
						.loadCrawlerfilestandardetailClassReferencedSet(crawlerfilestandardetail));
				// mav = editContinue(mav, crawlerfilestandardetail);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestandardetail/editCrawlerfilestandardetail.jsp");
			} else {
				crawlerfilestandardetailService.saveCrawlerfilestandardetail(crawlerfilestandardetail);
				mav.setViewName("forward:/listCrawlerfilestandardetails");
			}
		}
		// 编辑记录
		else {
			Crawlerfilestandardetail originCrawlerfilestandardetail = CmiHibernateProxyUtil
					.deproxy(crawlerfilestandardetailDAO
							.findCrawlerfilestandardetailByPrimaryKey(crawlerfilestandardetail.getId()));
			if (!crawlerfilestandardetail.equals(originCrawlerfilestandardetail)
					&& crawlerfilestandardetail.equals(existingCrawlerfilestandardetail)) {
				mav.addAllObjects(crawlerfilestandardetailService
						.loadCrawlerfilestandardetailClassReferencedSet(crawlerfilestandardetail));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerfilestandardetail/editCrawlerfilestandardetail.jsp");
			} else {
				crawlerfilestandardetailService.saveCrawlerfilestandardetail(crawlerfilestandardetail);
				mav.setViewName("forward:/listCrawlerfilestandardetails");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerfilestandardetailController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
